package org.ccay.security.entity;

import java.io.Serializable;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.ccay.core.request.IUserPrincipal;
import org.codehaus.jackson.annotate.JsonIgnoreProperties;

/**
 * 用户
 * @author chaowangbang
 * @since 2017年1月3日
 */
@JsonIgnoreProperties({ "password", "salt" })
public class User implements IUserPrincipal{
	
	private static final long serialVersionUID = 453249632628385412L;
	
	/**
	 * 主键
	 */
	private Long id;

	/**
	 * 登录的用户名及权限框架使用的用户名
	 */
	private String name;
	/**
	 * 显示名称--通常为业务相关的名称例如：工号
	 */
	private String displayName;
	
	/**
	 * email地址-用户收取邮件的地址
	 */
	private String email;
	/**
	 * 电话-用户收取短信的电话
	 */
	private String phone;
	
	/**
	 * <p>失效日期</p>
	 * 权限的失效日期
	 */
	private Date expireDate;
	/**
	 * <p>enabled属性主要是为安全控制而设计的，主要用于用户的认证与授权</p>
	 * <p>
	 * {@link #getEnabled()}在属于如下情况之一时返回false：
	 * <ui>
	 * 	<li>当前系统时间大于expireDate时间</li>
	 *  <li>enabled 本身值为false</li>
	 *  <li>deleted 值为true</li>
	 * </ui>
	 * </p>
	 */
	private Boolean enabled;
	

	/**
	 * <p>deleted属性主要是业务逻辑设计，用于实现逻辑删除</p>
	 */
	private Boolean deleted;
	/**
	 * 加密后的密码
	 */
	private String password;
	/**
	 * 用户名加密使用的盐
	 */
	private String salt;
	
	/**
	 * 当前登陆用户使用的主维度
	 * 在某些引用系统中,需要用户在主界面选择当前角色,当前部门等场景时用此集合记录
	 */
	private Map<String,Serializable> currentDimensions = new HashMap<String,Serializable>();
	

	/**
	 * 用户的扩展属性
	 */
	private Map<String,Serializable> attributes = new HashMap<String,Serializable> ();
	
	public User() {
	}
	
	public User(String name) {
		this.name = name;
	}
	
	public Long getId() {
		return id;
	}
	
	public void setId(Long id) {
		this.id = id;
	}
	
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getDisplayName() {
		return displayName;
	}

	public void setDisplayName(String displayName) {
		this.displayName = displayName;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getPhone() {
		return phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	public Date getExpireDate() {
		return expireDate;
	}

	public void setExpireDate(Date expireDate) {
		this.expireDate = expireDate;
	}

	public Boolean getDeleted() {
		return deleted;
	}

	public void setDeleted(Boolean deleted) {
		this.deleted = deleted;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getSalt() {
		return salt;
	}

	public void setSalt(String salt) {
		this.salt = salt;
	}
	
	/**
	 * 	在属于如下情况之一时返回false：
	 *  <ui>
	 * 	<li>当前系统时间大于expireDate时间</li>
	 *  <li>enabled 本身值为false</li>
	 *  <li>deleted 值为true</li>
	 */
	public Boolean getEnabled() {
		if(null != deleted && deleted){
			return false;
		}
		if(null != expireDate && expireDate.getTime() < System.currentTimeMillis()){
			return false;
		}
		return enabled;
	}

	public void setEnabled(Boolean enabled) {
		this.enabled = enabled;
	}

	public Map<String, Serializable> getAttributes() {
		return attributes;
	}

	public void setAttributes(Map<String, Serializable> attributes) {
		this.attributes = attributes;
	}
	
	public Map<String, Serializable> getCurrentDimensions() {
		return currentDimensions;
	}

	public void setCurrentDimensions(Map<String, Serializable> currentDimensions) {
		this.currentDimensions = currentDimensions;
	}
	/**
	 * 获取当前文档的文本表示
	 * @return
	 */
	public String currentDimensionsToString(){
		StringBuffer sf = new StringBuffer();
		for(Map.Entry<String, Serializable> entry:currentDimensions.entrySet()){
			sf.append(entry.getKey());
			sf.append(":");
			sf.append(entry.getValue().toString());
			sf.append(",");
		}
		if(!sf.toString().endsWith(","))
		{
			return "{}";
		}
		return "{"+sf.subSequence(0, sf.length()-1)+"}";
	}
	
	@Override
	public String toString() {
		return ""+name+"(" + id + ")" + currentDimensionsToString();
	}

	public Object getCacheKey() {
		StringBuffer cacheKey = new StringBuffer();
		cacheKey.append(name);
		if(null != currentDimensions){
			cacheKey.append(currentDimensionsToString());
		}
		return cacheKey.toString();
	}

	@Override
	public Long getUserId() {
		return id;
	}

	@Override
	public String getUserName() {
		return name;
	}

}
